package tp.pr5.instructions;

import tp.pr5.NavigationModule;
import tp.pr5.RobotEngine;
import tp.pr5.instructions.exceptions.InstructionExecutionException;
import tp.pr5.instructions.exceptions.WrongInstructionFormatException;
import tp.pr5.items.ItemContainer;

/**
 * Its execution moves the robot from one place to the next one in the current direction. 
 * This instruction works if the user writes MOVE or MOVER
 *
 */
public class MoveInstruction implements Instruction
{

	private NavigationModule navigation;
	private RobotEngine engine;
	
	/**
	 * Default constructor
	 */
	public MoveInstruction()
	{
		this.navigation = null;
	}
	
	/**
	 * Parses the String returning an instance of MoveInstruction or throwing a WrongInstructionFormatException()
	 * 
	 * @param cad string given
	 */
	@Override
	public Instruction parse(String cad) throws WrongInstructionFormatException 
	{
		cad = cad.toUpperCase();
		if(cad.equals("MOVE") || cad.equals("MOVER"))
		{
			return new MoveInstruction();
		}
		throw new WrongInstructionFormatException();
	}

	/**
	 * Fixes the current context, i.e., the robot engine and the navigation module
	 * 
	 * @param engine RobotEngine
	 * @param navigation NavigationModule
	 * @param robotContainer ItemContainer
	 * 
	 */
	@Override
	public void configureContext(RobotEngine engine, NavigationModule navigation, ItemContainer robotContainer)
	{
		this.navigation = navigation;
		this.engine = engine;
	}

	/**
	 * Moves from the current place to the next place on the current Direction. An opened street must exist between both places to be moved
	 */
	@Override
	public void execute() throws InstructionExecutionException 
	{
		try
		{
			this.navigation.move();
			this.engine.addFuel(-5);

		} catch(Exception e){
			throw e;
		}
	}

	/**
	 * Returns a description of the Instruction syntax. The string does not end with the line separator. It is up to the caller adding it before printing.
	 */
	@Override
	public String getHelp() 
	{
		return "MOVE|MOVER";
	}
	
	
}
